<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="35%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <div v-if="addEditFlag == 'add' || addEditFlag == 'edit'">
                <el-form-item label="设备维保标题" prop="title">
                    <el-input v-model="ruleForm.title" placeholder="请输入设备维保标题"></el-input>
                </el-form-item>
                <el-form-item label="设备编码" prop="deviceCode">
                    <el-select v-model="ruleForm.deviceCode" class="w100p" clearable filterable placeholder="请选择设备编码">
                        <el-option v-for="item, index in deviceList" :label="item.deviceCode" :value="item.deviceCode"
                            :key="index"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="维保描述" prop="remark">
                    <el-input type="textarea" :rows="2" v-model="ruleForm.remark" placeholder="请输入维保描述"></el-input>
                </el-form-item>
                <el-form-item label="维保日期" prop="maintainDate">
                    <el-date-picker v-model="ruleForm.maintainDate" align="right" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择日期" :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
            </div>
            <div v-else>
                <el-form-item label="设备维保标题:">
                    <span>{{ ruleForm.title }}</span>
                </el-form-item>
                <el-form-item label="设备编码:">
                    <span>{{ ruleForm.deviceCode }}</span>
                </el-form-item>
                <el-form-item label="维保描述:">
                    <span>{{ ruleForm.remark }}</span>
                </el-form-item>
                <el-form-item label="维保日期:">
                    <span>{{ ruleForm.maintainDate }}</span>
                </el-form-item>
                <el-form-item label="处理时间:">
                    <span>{{ ruleForm.handleTime }}</span>
                </el-form-item>
                <el-form-item label="处理意见:">
                    <span>{{ ruleForm.handleRemark }}</span>
                </el-form-item>
                <el-form-item v-if="ruleForm.maintainStatus == 'INPROGRESS'" label="处理时间" prop="handleTime">
                    <el-date-picker class="w100p" v-model="ruleForm.handleTime" value-format="yyyy-MM-dd HH:mm:ss"
                        type="datetime" placeholder="选择处理时间">
                    </el-date-picker>
                </el-form-item>
                <!-- <el-form-item label="处理状态" prop="maintainStatus">
                    <el-select v-model="ruleForm.maintainStatus" class="w100p" placeholder="请选择处理状态">
                        <el-option label="未处理" value="UNTREATED"></el-option>
                        <el-option label="进行中" value="INPROGRESS"></el-option>
                        <el-option label="结束" value="END"></el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item v-if="ruleForm.maintainStatus == 'INPROGRESS'" label="处理意见" prop="handleRemark">
                    <el-input type="textarea" :rows="2" v-model="ruleForm.handleRemark"
                        placeholder="请输入处理意见"></el-input>
                </el-form-item>
            </div>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button v-if="addEditFlag == 'add' || addEditFlag == 'edit'" type="primary" @click="submitForm()">确
                定</el-button>
            <template v-else>
                <el-button type="primary" v-if="ruleForm.maintainStatus == 'UNTREATED'"
                    @click="submitForm('INPROGRESS')">维保开始</el-button>
                <el-button type="primary" v-if="ruleForm.maintainStatus == 'INPROGRESS'"
                    @click="submitForm('END')">维保完成</el-button>
            </template>
        </span>
    </el-dialog>
</template>
<script>

import maintain from "@/api/maintain";
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        deviceList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            formData: {},
            addEditName: '添加',
            addEditApi: maintain.add,
            ruleForm: {},
            rules: {
                handleRemark: [
                    { required: true, message: '请输入处理意见', trigger: 'change' }
                ],
                handleFlag: [
                    { required: true, message: '请选择处理状态', trigger: 'change' }
                ],
                handleTime: [
                    { required: true, message: '请选择处理时间', trigger: 'change' }
                ],
                remark: [
                    { required: true, message: '请输入设备描述', trigger: 'change' }
                ],
                title: [
                    { required: true, message: '请输入设备维保标题', trigger: 'change' }
                ],
                maintainDate: [
                    { required: true, message: '请选择安维保日期', trigger: 'change' }
                ],
                deviceCode: [
                    { required: true, message: '请选择设备编码', trigger: 'change' }
                ],
                maintainStatus: [
                    { required: true, message: '请选择设备编码', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    computed: {},
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = maintain.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = maintain.edit
                this.addEditName = '编辑'
            } else if (this.addEditFlag == 'handle') {

                this.ruleForm = {
                    ...this.data,
                    handleUserId: sessionStorage.userId
                }
                this.addEditApi = maintain.handle
                this.addEditName = '处理'
            }
        },
        //提交表单
        submitForm(falg) {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = falg ? {
                        ...this.ruleForm,
                        maintainStatus: falg
                    } : this.ruleForm;
                    this.addEditApi({
                        ...params
                    }).then(res => {
                        if (res.code == 1) {
                            this.cancel()
                            this.$message.success('操作成功')
                        } else {
                            this.$message.warning(res.msg || '操作失败')
                        }

                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}
</style>